home *** CD-ROM | disk | FTP | other *** search
- /*
- Star Chart
- Originally written in DeSmet C for the Macintosh by
- David Palmer
- Mail Code 220-47
- California Institute Of Technology
- Pasadena CA, 91125
-
- The Data File is in the public domain
- This Program is copyright 1986 By David Palmer,
- It may be copied, modified, or given away, but not sold.
- Anybody making substantial improvements must distribute the improved version
- under the same conditions, and send me a copy of the improved program.
- */
-
- #include <quickdraw.h>
- #include <window.h>
- #include <menu.h>
- #include <event.h>
- #include <textedit.h>
- #include <dialog.h>
- #include <stdio.h>
- #include <math.h>
- #include "star.h"
-
- int nstars = 0;
-
- star *cat;
- double atof();
-
- #define Bool10(f) (f ? 1 : 0)
-
- /* DeSmet C bug: DeSmet C, when casting floats into integers, rounds instead */
- /* of truncating, a flagrant violation of the standard, for which the author */
- /* should be forced to use BASIC */
- int makeint(i)
- double i;
- {
- if ((i > 0 && i > (int) i) || (i < 0 && i < (int) i))
- return (int) i;
- else if (i > 0)
- return (int)i - 1;
- else if (i < 0)
- return (int)i + 1;
- }
-
- InitAll()
- {
- InitGraf(&thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- if (-1 == OpenResFile(RESFILE)) {
- printf("Cannot open resource file\n");
- sysbeep(20);
- exit();
- }
- GetCat(STARFILE);
- }
-
- GetCat(fName)
- char *fName;
- {
- FILE *fcat, *fopen();
-
- nstars = NSTARS;
- cat = lmalloc((long)NSTARS * (long)sizeof(star));
- if (cat == NULL) {
- printf("Not enough memory to run stars\n");
- sysbeep(20);
- exit();
- }
-
- fcat = fopen(fName, "r");
- if (fcat == NULL) {
- printf("File not openable\n");
- exit();
- }
- setbinary(*fcat); /* Do not map CR=>CRLF or vice-versa */
- fread(cat, nstars, sizeof(star), fcat);
- fclose(fcat);
- }
-
- DrawMap(pg, philook, thetalook, size, maglabel, fblack, fcross, flabel)
- GrafPort *pg;
- double philook, thetalook, size; /* All in radians */
- int fblack, fcross;
- int maglabel, flabel;
- {
- float scale;
- long u, v, w; /* coordinates in right, up, forward directions */
- long forward[3], right[3], up[3];
- int xmax, ymax, xcenter, ycenter;
- int i;
- Rect rc;
-
- TextFont(1);
- TextSize(9);
- TextMode(srcXor);
- forward[X] = DIST * sin(thetalook) * cos(philook);
- forward[Y] = DIST * sin(thetalook) * sin(philook);
- forward[Z] = DIST * cos(thetalook);
- up[X] = DIST * sin(thetalook - (PI/2)) * cos(philook);
- up[Y] = DIST * sin(thetalook - (PI/2)) * sin(philook);
- up[Z] = DIST * cos(thetalook - (PI/2));
- right[X] = (forward[Y] * up[Z] - forward[Z] * up[Y])/DIST;
- right[Y] = (forward[Z] * up[X] - forward[X] * up[Z])/DIST;
- right[Z] = (forward[X] * up[Y] - forward[Y] * up[X])/DIST;
-
- PenNormal();
- PaintRect(pg->portRect);
- if (!fblack)
- InvertRect(pg->portRect);
- xmax = pg->portRect.right;
- ymax = pg->portRect.bottom;
- scale = ((float)xmax)/(3.2e4 * 2 * sin(size/2));
- xcenter = xmax/2;
- ycenter = ymax/2;
- for (i = 0 ; i < nstars ; i++) {
- w = ((long)cat[i].x * forward[X] + (long)cat[i].y * forward[Y] +
- (long)cat[i].z * forward[Z])/DIST;
- if (0. < (float)w) {
- /* DeSmet C bug: If the "+-" in the next line is changed to a "-", strange things */
- /* happen, due to a DeSmet compiler bug (at least in version 1.01) */
- v = ycenter +- (scale * ((long)cat[i].x * up[X] +
- (long)cat[i].y * up[Y] +(long)cat[i].z * up[Z]))/w;
- if (0 < v && v < ymax) {
- u = xcenter + (scale * ((long)cat[i].x * right[X] +
- (long)cat[i].y * right[Y] + (long)cat[i].z * right[Z]))/w;
- if (0 < u && u < xmax) {
- rc.left = u - (700 - cat[i].mag)/200;
- rc.right = rc.left + (700 - cat[i].mag)/100;
- rc.top = v - (700 - cat[i].mag)/200;
- rc.bottom = rc.top + (700 - cat[i].mag)/100;
- invertoval(rc);
- if (flabel && cat[i].mag < maglabel) {
- MoveTo(rc.right+2, rc.bottom);
- DrawText(&cat[i].name , 0, 6);
- }
- }
- }
- }
- }
- penmode(patXor);
- if (fcross) {
- moveto(xcenter - 5, ycenter); lineto(xcenter + 5, ycenter);
- moveto(xcenter, ycenter - 5); lineto(xcenter, ycenter + 5);
- }
- }
-
- GetNText(pdi, itemno, pch)
- DialogPtr pdi;
- int itemno;
- char *pch;
- {
- long type;
- Handle item;
- Rect box;
-
- GetDItem(pdi, itemno, &type, &item, &box);
- GetIText(item, pch);
- }
-
- SetNText(pdi, itemno, pch)
- DialogPtr pdi;
- int itemno;
- char *pch;
- {
- long type;
- Handle item;
- Rect box;
-
- GetDItem(pdi, itemno, &type, &item, &box);
- SetIText(item, pch);
- }
-
- SetNValue(pdi, itemno, value)
- DialogPtr pdi;
- int itemno;
- int value;
- {
- long type;
- Handle item;
- Rect box;
-
- GetDItem(pdi, itemno, &type, &item, &box);
- SetCtlValue(item, value);
- }
-
- int GetNValue(pdi, itemno)
- DialogPtr pdi;
- int itemno;
- {
- long type;
- Handle item;
- Rect box;
-
- GetDItem(pdi, itemno, &type, &item, &box);
- return GetCtlValue(item);
- }
-
- SetNhms(pdi, ci, itemno, angle) /* ci is number of items, angle is decimal hours */
- DialogPtr pdi;
- int ci, itemno;
- double angle;
- {
- int unit;
- char rgch[10];
- int i, sig = 1;
- double twiddle = 0.5;
-
- if (angle < 0) {
- sig = -1;
- angle = -angle;
- }
-
- for (i = 1 ; i < ci ; i++)
- twiddle /= 60.;
- angle += twiddle;
- for (i = 0 ; i < ci ; i++) {
- unit = makeint(angle);
- sprintf(rgch, "%d", unit);
- SetNText(pdi, itemno + i, rgch);
- angle = 60 * (angle - unit);
- }
- }
-
- double GetNhms(pdi, ci, itemno)
- DialogPtr pdi;
- int ci, itemno;
- {
- char rgch[257];
- double angle = 0;
- int i;
- int sig = 1;
- double fraction = 1;
-
- for (i = 0 ; i < ci ; i++) {
- GetNText(pdi, itemno+i, rgch);
- angle += atof(rgch)*fraction;
- if (angle < 0 || (i == 0 && rgch[0] == '-')) {
- sig = -1;
- angle = fabs(angle);
- }
- fraction /= 60;
- }
- return sig*angle;
- }
-
- int DoDialog(pphi, ptheta, pwidth, pmaglabel, pfblack, pfcross, pflabel)
- double *pphi, *ptheta, *pwidth;
- int *pmaglabel, *pflabel;
- int *pfblack, *pfcross;
- {
- DialogPtr pdi, pdi2;
- double phi, theta;
- int itemhit, item2;
- char rgch[257];
- int sig = 1;
-
- pdi = GetNewDialog(DIDget, 0l, -1l);
-
- SetNhms(pdi, 3, TErah, RtoH * *pphi);
- theta = PIo2 + 1.e-7 - *ptheta;
- SetNValue(pdi, RBn, Bool10(theta > 0));
- SetNValue(pdi, RBs, Bool10(theta <= 0));
- SetNhms(pdi, 3, TEdecd, RtoD * fabs(theta));
- SetNhms(pdi, 1, TEwid, RtoD * *pwidth);
- sprintf(rgch, "%0.2f", (double)*pmaglabel /100.);
- SetNText(pdi, 16, rgch);
- SetNValue(pdi, CBblack, Bool10(*pfblack));
- SetNValue(pdi, CBcross, Bool10(*pfcross));
- SetNValue(pdi, CBlabel, Bool10(*pflabel));
-
- do {
- ModalDialog(0l, &itemhit);
- switch (itemhit) {
- case BUTok:
- case BUTcan:
- break;
- case BUTblame:
- pdi2 = GetNewDialog(DIDblame, 0l, -1l);
- do {
- ModalDialog(0l, &item2);
- } while (item2 != BUTok);
- CloseDialog(pdi2);
- break;
- case CBlabel:
- case CBcross:
- case CBblack:
- SetNValue(pdi, itemhit, (GetNValue(pdi, itemhit) ? 0 : 1));
- break;
- case RBn:
- case RBs:
- SetNValue(pdi, RBn, Bool10(!(itemhit & 1)));
- SetNValue(pdi, RBs, Bool10(itemhit & 1));
- case TEmaglabel:
- default:
- break;
- }
- } while (itemhit != BUTok && itemhit != BUTcan);
- if (itemhit == BUTcan) return 0;
- *pfcross = GetNValue(pdi, CBcross);
- *pfblack = GetNValue(pdi, CBblack);
- *pflabel = GetNValue(pdi, CBlabel);
- *pphi = GetNhms(pdi, 3, TErah) * HtoR;
- *pwidth = DtoR * GetNhms(pdi, 1, TEwid);
- *pmaglabel = 100 * GetNhms(pdi, 1, TEmaglabel);
- theta = GetNhms(pdi, 3, TEdecd) * DtoR;
- if (GetNValue(pdi, RBn))
- *ptheta = PIo2 - theta;
- else
- *ptheta = PIo2 + theta;
- CloseDialog(pdi);
- return -1;
- }
-
- main()
- {
- EventRecord theEvent;
- double phi = 1.43990,
- theta = 1.570796,
- width = 1.047;
- int fblack = 1,
- fcross = 1;
- int maglabel = 200,
- flabel = 0;
-
- InitAll();
- FlushEvents(0xffff, 0);
- while (DoDialog(&phi, &theta, &width, &maglabel, &fblack, &fcross, &flabel)) {
- HideCursor();
- DrawMap(thePort, phi, theta, width, maglabel, fblack, fcross, flabel);
- ShowCursor();
- ObscureCursor(); /* Keep the cursor out of sight */
- while (!Button()) GetNextEvent(0xffff, &theEvent);
- while (Button()) ;
- FlushEvents(0xffff, 0);
- }
- }